# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>
# Contributor: Biswapriyo Nath <nathbappai@gmail.com>

_realname=grpc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.57.0
pkgrel=1
_opencensus_proto_ver=0.4.1
pkgdesc="Google's high performance, open source, general RPC framework (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://www.grpc.io/'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-abseil-cpp"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-re2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/grpc/grpc/archive/v${pkgver}.tar.gz"
        "opencensus-proto-${_opencensus_proto_ver}.tar.gz::https://github.com/census-instrumentation/opencensus-proto/archive/v${_opencensus_proto_ver}.tar.gz"
        "001-fix-building-shared-libraries-with-clang.patch"
        "002-fix-grpcConfig.cmake.patch")
sha256sums=('8393767af531b2d0549a4c26cf8ba1f665b16c16fb6c9238a7755e45444881dd'
            'e3d89f7f9ed84c9b6eee818c2e9306950519402bf803698b15c310b77ca2f0f3'
            '022f6424e274620f5337b56fc46217607496870ed5fb197413a44c83961ae388'
            '2764d9f6f08511f54e1153ebe350a91a251f6f8a335ad8903dea161bd432d27d')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  tar -xzf "${srcdir}"/${_realname}-${pkgver}.tar.gz -C "${srcdir}" || true

  cd "${srcdir}"/${_realname}-${pkgver}
  # Suggested by @mstorsjo in https://github.com/msys2/MINGW-packages/pull/16378#issuecomment-1481002915
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    patch -p1 -i "${srcdir}"/001-fix-building-shared-libraries-with-clang.patch
  fi

  # https://github.com/grpc/grpc/pull/33361
  patch -p1 -i "${srcdir}"/002-fix-grpcConfig.cmake.patch

  rm -rf third_party/opencensus-proto
  mv "${srcdir}"/opencensus-proto-${_opencensus_proto_ver} third_party/opencensus-proto
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # remove __USE_MINGW_ANSI_STDIO=1 define, it breaks grpc string handling for
  # cases like 'gpr_log(GPR_DEBUG, "Failed to free %" PRIuPTR ...'
  CPPFLAGS="${CPPFLAGS//-D__USE_MINGW_ANSI_STDIO=1/}"
  # add STRSAFE_NO_DEPRECATE define, otherwise strsafe breaks libc++ headers
  CXXFLAGS+=" -DSTRSAFE_NO_DEPRECATE"
  case "${CARCH}" in
    x86_64)
      # https://github.com/msys2/MINGW-packages/issues/8984
      LDFLAGS+=" -Wl,--disable-dynamicbase,--default-image-base-low"
    ;;
  esac

  CXXFLAGS+=" -w"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_CXX_STANDARD=17 \
      -DCMAKE_SKIP_RPATH=ON \
      -DgRPC_ZLIB_PROVIDER="package" \
      -DgRPC_CARES_PROVIDER="package" \
      -DgRPC_RE2_PROVIDER="package" \
      -DgRPC_SSL_PROVIDER="package" \
      -DgRPC_PROTOBUF_PROVIDER="package" \
      -DgRPC_ABSL_PROVIDER="package" \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  #${MINGW_PREFIX}/bin/cmake.exe --build . --target test
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
